
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 08/12/2013 12:39:35
-- Generated from EDMX file: D:\MvcApplication1\EquipmentManager\Entities\Model.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [d:\MvcApplication1\EquipmentManager\App_Data\DB.mdf];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_DirectorateRegion]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EntityBaseSet_Region] DROP CONSTRAINT [FK_DirectorateRegion];
GO
IF OBJECT_ID(N'[dbo].[FK_PostOfficeRegion]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EntityBaseSet_PostOffice] DROP CONSTRAINT [FK_PostOfficeRegion];
GO
IF OBJECT_ID(N'[dbo].[FK_UserRegion]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EntityBaseSet_User] DROP CONSTRAINT [FK_UserRegion];
GO
IF OBJECT_ID(N'[dbo].[FK_EquipmentBasePostOffice]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EntityBaseSet_EquipmentBase] DROP CONSTRAINT [FK_EquipmentBasePostOffice];
GO
IF OBJECT_ID(N'[dbo].[FK_Region_inherits_EntityBase]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EntityBaseSet_Region] DROP CONSTRAINT [FK_Region_inherits_EntityBase];
GO
IF OBJECT_ID(N'[dbo].[FK_Directorate_inherits_EntityBase]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EntityBaseSet_Directorate] DROP CONSTRAINT [FK_Directorate_inherits_EntityBase];
GO
IF OBJECT_ID(N'[dbo].[FK_PostOffice_inherits_EntityBase]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EntityBaseSet_PostOffice] DROP CONSTRAINT [FK_PostOffice_inherits_EntityBase];
GO
IF OBJECT_ID(N'[dbo].[FK_User_inherits_EntityBase]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EntityBaseSet_User] DROP CONSTRAINT [FK_User_inherits_EntityBase];
GO
IF OBJECT_ID(N'[dbo].[FK_EquipmentBase_inherits_EntityBase]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EntityBaseSet_EquipmentBase] DROP CONSTRAINT [FK_EquipmentBase_inherits_EntityBase];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[EntityBaseSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EntityBaseSet];
GO
IF OBJECT_ID(N'[dbo].[EntityBaseSet_Region]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EntityBaseSet_Region];
GO
IF OBJECT_ID(N'[dbo].[EntityBaseSet_Directorate]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EntityBaseSet_Directorate];
GO
IF OBJECT_ID(N'[dbo].[EntityBaseSet_PostOffice]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EntityBaseSet_PostOffice];
GO
IF OBJECT_ID(N'[dbo].[EntityBaseSet_User]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EntityBaseSet_User];
GO
IF OBJECT_ID(N'[dbo].[EntityBaseSet_EquipmentBase]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EntityBaseSet_EquipmentBase];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'EntityBaseSet'
CREATE TABLE [dbo].[EntityBaseSet] (
    [Id] bigint IDENTITY(1,1) NOT NULL,
    [Status] tinyint  NOT NULL
);
GO

-- Creating table 'EntityBaseSet_Region'
CREATE TABLE [dbo].[EntityBaseSet_Region] (
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [Id] bigint  NOT NULL,
    [Directorate_Id] bigint  NOT NULL
);
GO

-- Creating table 'EntityBaseSet_Directorate'
CREATE TABLE [dbo].[EntityBaseSet_Directorate] (
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [Id] bigint  NOT NULL
);
GO

-- Creating table 'EntityBaseSet_PostOffice'
CREATE TABLE [dbo].[EntityBaseSet_PostOffice] (
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [Id] bigint  NOT NULL,
    [Region_Id] bigint  NOT NULL
);
GO

-- Creating table 'EntityBaseSet_User'
CREATE TABLE [dbo].[EntityBaseSet_User] (
    [UserName] nvarchar(max)  NOT NULL,
    [Password] nvarchar(max)  NOT NULL,
    [Role] tinyint  NOT NULL,
    [FullName] nvarchar(max)  NOT NULL,
    [RegistredDate] datetime  NOT NULL,
    [Id] bigint  NOT NULL,
    [Region_Id] bigint  NOT NULL
);
GO

-- Creating table 'EntityBaseSet_EquipmentBase'
CREATE TABLE [dbo].[EntityBaseSet_EquipmentBase] (
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Number] nvarchar(max)  NOT NULL,
    [Note] nvarchar(max)  NULL,
    [Year] smallint  NOT NULL,
    [State] tinyint  NOT NULL,
    [CreatedDate] datetime  NOT NULL,
    [SerialNumber] nvarchar(max)  NOT NULL,
    [WorkPlace] nvarchar(max)  NOT NULL,
    [Id] bigint  NOT NULL,
    [PostOffice_Id] bigint  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'EntityBaseSet'
ALTER TABLE [dbo].[EntityBaseSet]
ADD CONSTRAINT [PK_EntityBaseSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'EntityBaseSet_Region'
ALTER TABLE [dbo].[EntityBaseSet_Region]
ADD CONSTRAINT [PK_EntityBaseSet_Region]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'EntityBaseSet_Directorate'
ALTER TABLE [dbo].[EntityBaseSet_Directorate]
ADD CONSTRAINT [PK_EntityBaseSet_Directorate]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'EntityBaseSet_PostOffice'
ALTER TABLE [dbo].[EntityBaseSet_PostOffice]
ADD CONSTRAINT [PK_EntityBaseSet_PostOffice]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'EntityBaseSet_User'
ALTER TABLE [dbo].[EntityBaseSet_User]
ADD CONSTRAINT [PK_EntityBaseSet_User]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'EntityBaseSet_EquipmentBase'
ALTER TABLE [dbo].[EntityBaseSet_EquipmentBase]
ADD CONSTRAINT [PK_EntityBaseSet_EquipmentBase]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Directorate_Id] in table 'EntityBaseSet_Region'
ALTER TABLE [dbo].[EntityBaseSet_Region]
ADD CONSTRAINT [FK_DirectorateRegion]
    FOREIGN KEY ([Directorate_Id])
    REFERENCES [dbo].[EntityBaseSet_Directorate]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DirectorateRegion'
CREATE INDEX [IX_FK_DirectorateRegion]
ON [dbo].[EntityBaseSet_Region]
    ([Directorate_Id]);
GO

-- Creating foreign key on [Region_Id] in table 'EntityBaseSet_PostOffice'
ALTER TABLE [dbo].[EntityBaseSet_PostOffice]
ADD CONSTRAINT [FK_PostOfficeRegion]
    FOREIGN KEY ([Region_Id])
    REFERENCES [dbo].[EntityBaseSet_Region]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PostOfficeRegion'
CREATE INDEX [IX_FK_PostOfficeRegion]
ON [dbo].[EntityBaseSet_PostOffice]
    ([Region_Id]);
GO

-- Creating foreign key on [Region_Id] in table 'EntityBaseSet_User'
ALTER TABLE [dbo].[EntityBaseSet_User]
ADD CONSTRAINT [FK_UserRegion]
    FOREIGN KEY ([Region_Id])
    REFERENCES [dbo].[EntityBaseSet_Region]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserRegion'
CREATE INDEX [IX_FK_UserRegion]
ON [dbo].[EntityBaseSet_User]
    ([Region_Id]);
GO

-- Creating foreign key on [PostOffice_Id] in table 'EntityBaseSet_EquipmentBase'
ALTER TABLE [dbo].[EntityBaseSet_EquipmentBase]
ADD CONSTRAINT [FK_EquipmentBasePostOffice]
    FOREIGN KEY ([PostOffice_Id])
    REFERENCES [dbo].[EntityBaseSet_PostOffice]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EquipmentBasePostOffice'
CREATE INDEX [IX_FK_EquipmentBasePostOffice]
ON [dbo].[EntityBaseSet_EquipmentBase]
    ([PostOffice_Id]);
GO

-- Creating foreign key on [Id] in table 'EntityBaseSet_Region'
ALTER TABLE [dbo].[EntityBaseSet_Region]
ADD CONSTRAINT [FK_Region_inherits_EntityBase]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[EntityBaseSet]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'EntityBaseSet_Directorate'
ALTER TABLE [dbo].[EntityBaseSet_Directorate]
ADD CONSTRAINT [FK_Directorate_inherits_EntityBase]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[EntityBaseSet]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'EntityBaseSet_PostOffice'
ALTER TABLE [dbo].[EntityBaseSet_PostOffice]
ADD CONSTRAINT [FK_PostOffice_inherits_EntityBase]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[EntityBaseSet]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'EntityBaseSet_User'
ALTER TABLE [dbo].[EntityBaseSet_User]
ADD CONSTRAINT [FK_User_inherits_EntityBase]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[EntityBaseSet]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'EntityBaseSet_EquipmentBase'
ALTER TABLE [dbo].[EntityBaseSet_EquipmentBase]
ADD CONSTRAINT [FK_EquipmentBase_inherits_EntityBase]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[EntityBaseSet]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------